"""
xapper test app functional test definitions.
"""

import zipfile
try:
  from cStringIO import StringIO
except ImportError:
  from StringIO import StringIO

import django, testapp.xaps, xapper
from django.core.urlresolvers import reverse


class FunctionalTests(django.test.TestCase):

  def test_xap(self):
    app_a_url = reverse(xapper.views.xap, args=("testapp.app_a",))
    app_b_url = reverse(xapper.views.xap, args=("testapp.app_b",))
    response = self.client.get(app_a_url)
    self.assertEqual(response["Content-Type"], "application/x-silverlight-app")
    xapbuf = StringIO(response.content)
    zf = zipfile.ZipFile(xapbuf)
    xap_contents = zf.namelist()
    for assembly in xapper.conf.settings.XAPPER_DEFAULT_ASSEMBLIES:
      assert assembly in xap_contents
    assert "AppManifest.xaml" in xap_contents
    assert "app.py" in xap_contents
    assert "app.xaml" in xap_contents

    response = self.client.get(app_b_url)
    self.assertEqual(response["Content-Type"], "application/x-silverlight-app")
    xapbuf = StringIO(response.content)
    zf = zipfile.ZipFile(xapbuf)
    xap_contents = zf.namelist()
    assert "IronPython.dll" not in xap_contents
    assert "IronPython.Modules.dll" not in xap_contents
    assert "AppManifest.xaml" in xap_contents
    assert "a" in xap_contents
    assert "b" in xap_contents
    assert "c" in xap_contents
    assert "d" in xap_contents

  def test_app_names(self):
    app_a = testapp.xaps.app_a
    app_b = testapp.xaps.app_b
    self.assertEqual(app_a.name, "testapp.app_a")
    self.assertEqual(app_b.name, "testapp.app_b")
    app_a_url = reverse(xapper.views.xap, args=("testapp.app_a",))
    app_b_url = reverse(xapper.views.xap, args=("testapp.app_b",))
    self.assertNotEqual(app_a_url, app_b_url)
    self.assertEqual(app_a.url, app_a_url)
    self.assertEqual(app_b.url, app_b_url)
